package com.xdf.ecommerce.service.impl;

import com.xdf.ecommerce.account.AddressInfo;
import com.xdf.ecommerce.common.TableId;
import com.xdf.ecommerce.dao.EcommerceAddressDao;
import com.xdf.ecommerce.entity.EcommerceAddress;
import com.xdf.ecommerce.filter.AccessContext;
import com.xdf.ecommerce.service.IAddressService;
import com.xdf.ecommerce.vo.LoginUserInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chanchaw
 * @create 2025-10-25 17:26
 */
@RequiredArgsConstructor
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class AddressServiceImpl implements IAddressService {
    private final EcommerceAddressDao addressDao;

    /**
     * 注意，创建地址时不使用传入参数 addressInfo 中的 userId
     * 而是使用系统自己保存下来的当前登录的用户，防止前端传递来的数据被篡改后
     * 导致创建了虚假的数据
     * @param addressInfo 只使用其中的地址属性，不使用 userId
     * @return
     */
    @Override
    public TableId createAddressInfo(AddressInfo addressInfo) {
        LoginUserInfo loginUserInfo = AccessContext.getLoginUserInfo();// 获取当前登录的用户
        List<EcommerceAddress> ecommerceAddressList = addressInfo.getAddressItems().stream().map(a -> EcommerceAddress.to(loginUserInfo.getId(), a)).collect(Collectors.toList());

        List<TableId.Id> ids = new ArrayList<>();
        for (EcommerceAddress item : ecommerceAddressList) {
            int i = addressDao.insertSelective(item);
            ids.add(new TableId.Id(item.getId()));
        }
        TableId tableId = new TableId();
        tableId.setIds(ids);
        return tableId;
    }

    @Override
    public AddressInfo getCurrentAddressInfo() {
        LoginUserInfo loginUserInfo = AccessContext.getLoginUserInfo();
        List<EcommerceAddress> addressList = addressDao.findAllByUserId(loginUserInfo.getId());

        List<AddressInfo.AddressItem> list = new ArrayList<>();
        for (EcommerceAddress item : addressList) {
            AddressInfo.AddressItem addressItem = EcommerceAddress.toAddressItem(item);
            list.add(addressItem);
        }

        AddressInfo addressInfo = new AddressInfo();
        addressInfo.setUserId(loginUserInfo.getId());
        addressInfo.setAddressItems(list);
        return addressInfo;
    }

    @Override
    public AddressInfo getAddressInfoById(Long id) {
        EcommerceAddress ecommerceAddress = addressDao.selectOneById(id);
        AddressInfo.AddressItem addressItem = EcommerceAddress.toAddressItem(ecommerceAddress);

        List<AddressInfo.AddressItem> list = new ArrayList<>();
        list.add(addressItem);
        return new AddressInfo(ecommerceAddress.getUserId(), list);
    }

    @Override
    public AddressInfo getAddressInfoByTableId(TableId tableId) {
        List<Long> ids = tableId.getIds().stream().map(TableId.Id::getId).collect(Collectors.toList());
        List<EcommerceAddress> ecommerceAddressList = addressDao.selectListByIds(ids);

        Long userId = ecommerceAddressList.get(0).getUserId();
        List<AddressInfo.AddressItem> addressItemList = ecommerceAddressList.stream().map(EcommerceAddress::toAddressItem).collect(Collectors.toList());
        return new AddressInfo(userId, addressItemList);
    }
}
